package com.yuan.yblog.portal.pojo.VO;

import com.yuan.yblog.crud.entity.Comment;
import com.yuan.yblog.crud.entity.Post;
import com.yuan.yblog.portal.pojo.contants.Status;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;


/**
 * @author hepeiyuan on 2024/5/13 16:43
 * @description
 */
@EqualsAndHashCode(callSuper = true)
@Data
@Schema(description = "评论实体")
public class CommentVO extends Comment {

    @Schema(description = "评论者昵称")
    private String username;

    @Schema(description = "评论者头像")
    private String userAvatar;

    @Schema(description = "被评论/回复的主题内容，如：文章标题、被回复的评论内容")
    private String passiveContent;


    public Comment toInsertEntity(){
        Comment comment = new Comment();
        comment.setId(null);
        comment.setContent(this.getContent());
        comment.setParentId(this.getParentId());
        comment.setPostId(this.getPostId());
        comment.setUserId(this.getUserId());
        comment.setVoteUp(0);
        comment.setVoteDown(0);
        comment.setLevel(100);
        comment.setStatus(Status.valid);
        comment.setCreated(new Date());
        comment.setModified(new Date());
        return comment;
    }

    public Comment toUpdateEntity(){
        Comment comment = new Comment();
        comment.setId(this.getPostId());
        comment.setContent(this.getContent());
        comment.setParentId(null);
        comment.setPostId(null);
        comment.setUserId(null);
        comment.setVoteUp(null);
        comment.setVoteDown(null);
        comment.setLevel(null);
        comment.setStatus(null);
        comment.setCreated(null);
        comment.setModified(new Date());
        return comment;
    }

    public static CommentVO getCommentVO(Comment comment, String passiveContent) {
        CommentVO commentVO = new CommentVO();
        if (comment != null) {
            commentVO.setId(comment.getPostId());
            commentVO.setContent(comment.getContent());
            commentVO.setParentId(comment.getParentId());
            commentVO.setPostId(comment.getPostId());
            commentVO.setUserId(comment.getUserId());
            commentVO.setVoteUp(comment.getVoteUp());
            commentVO.setVoteDown(comment.getVoteDown());
            commentVO.setLevel(comment.getLevel());
            commentVO.setStatus(comment.getStatus());
            commentVO.setCreated(comment.getCreated());
            commentVO.setModified(comment.getModified());
        }
        if(passiveContent != null) {
            commentVO.setPassiveContent(passiveContent);
        }
        return commentVO;
    }

}
